﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using AcademicPlanner.Framework.Helpers;

namespace AcademicPlanner.Presentation
{
	/// <summary>
	/// RequireAuthenticationAttribute represents an Mvc Controller filter
	/// for making sure the session user is authenticated before access to a
	/// Controller's action.
	/// </summary>
	/// <since>1.0</since>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
	public class RequireAuthenticationAttribute : FilterAttribute, IActionFilter
	{
		#region IActionFilter Members

		void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
		{
			
		}

		/// <summary>
		/// Checks that the session user is authenticated and in case
		/// of failure redirects to the home page for the user to login.
		/// </summary>
		/// <remarks>
		/// The session user being authenticated is not the same as
		/// him being authentic. Authenticated means he has signed in
		/// successfully which  requires him to authentic but still,
		/// whether the session user is authentic or not is determined by 
		/// the domain layer; the presentation layer just caches that
		/// information during the signin process.
		/// </remarks>
		/// <param name="filterContext">The context of filtering.</param>
		/// <seealso cref="M:AcademicPlanner.Framework.Helpers.SessionHelper.IsUserAuthenticated"/>
		/// <since>1.0</since>
		public void OnActionExecuting(ActionExecutingContext filterContext)
		{
			if (!SessionHelper.IsUserAuthenticated())
			{
				ActionResult vResult;

				if (filterContext.HttpContext.Request.IsAjaxRequest())
				{
					vResult = new ContentResult();
					((ContentResult)vResult).ContentType = "text/html";
					((ContentResult)vResult).Content = "<script>window.location.href = window.location.href.replace(/#.*$/, '');</script>";
				}
				else
				{
				SessionHelper.AddInfoStatusMessage(TranslationHelper
					.Translate("Please login in order to access that page!", "Presentation"));
				SessionHelper.Reset("Presentation.LoginRedirection", filterContext.RouteData.Values);
					vResult = new RedirectToRouteResult(new RouteValueDictionary(new
				{
					controller = "Site",
					action = "Default"
				}));
			}

				filterContext.Result = vResult; 
		}
		}

		#endregion
	}
}
